package com.cskaoyan.service.goods;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.goods.Comment;
import com.cskaoyan.bean.goods.CommentExample;
import com.cskaoyan.bean.goods.base.CommentPageData;
import com.cskaoyan.bean.goods.bo.CommentSearchBO;
import com.cskaoyan.mapper.goods.CommentMapper;
import com.cskaoyan.utils.Constant;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @Description:商品评论接口的实现类
 * @author: Jason 805636901@qq.com
 * @create: 2021-10-19-8:19 PM
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentMapper commentMapper;

    @Override
    public CommentPageData queryComment(BaseParam baseParam, CommentSearchBO commentSearchBO) {
        //配置分页信息
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());


        CommentExample example = new CommentExample();
        CommentExample.Criteria criteria = example.createCriteria();

        //查询条件
        if (commentSearchBO.getUserId() != null && !"".equals(commentSearchBO.getUserId())) {
            criteria.andIdEqualTo(commentSearchBO.getUserId());
        }
        if (commentSearchBO.getValueId() != null && !"".equals(commentSearchBO.getValueId())) {
            criteria.andValueIdEqualTo(commentSearchBO.getValueId());
        }


        criteria.andDeletedEqualTo(false);
        //设置排序
        example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<Comment> comments = commentMapper.selectByExample(example);
        return CommentPageData.ok(comments);
    }

    @Override
    public int deleteComment(Comment comment) {
        //逻辑删除,将deleted 由 false 👉 true
        //数据库中 0 对应 false, 0为存在 ,1为不存在
        if (comment.getDeleted())return Constant.FAIL;
        comment.setDeleted(true);
        int i = commentMapper.updateByPrimaryKeySelective(comment);
        if (i == 1) return Constant.SUCCESS;
        return Constant.FAIL;
    }
}
